package com.ruitu.health_2.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.CommonModel;
import com.ruitu.health_2.mvp.presenter.CommonPresenter;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.health_2.ui.common.ArticleListActivity;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.home.AdvertContent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 通用fragment文章列表  八个模块下通用文章列表
public class ComArticleListFragment extends BaseFragment<CommonPresenter.ComArticleListP, CommonModel.ComArticleListM> {
    private RecyclerView rcv_com_article_list;// 文章列表
    private TextView tv_more;// 更多

    private List<Article> articleList = new ArrayList<>();// 文章列表
    private ModelArticleListAdapter articleAdapter;
    private static String posId = "";// 传过来的tab posId

    public static ComArticleListFragment newInstance(String posId) {
        ComArticleListFragment.posId = posId;
        Bundle args = new Bundle();
        ComArticleListFragment fragment = new ComArticleListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsg(EventModel ev) {
        if (ev.getEventCode() == 20011351) {

        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);
        articleAdapter = new ModelArticleListAdapter(getActivity());

        articleAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(getActivity(), "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.com_article_list_fragment, container, false);

        rcv_com_article_list = v.findViewById(R.id.rcv_com_article_list);
        tv_more = v.findViewById(R.id.tv_more);
        setOnClickListener(tv_more);

        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_com_article_list.setLayoutManager(manager);
        rcv_com_article_list.setAdapter(articleAdapter);

        reqData();
        hideProgress();
        showProgress();

        return v;
    }

    @Override
    public void onClick(View view) {
        if (view == tv_more) {// 更多
            if (!isDataEmpty(articleList)) {
                startActivity(ArticleListActivity.class);
            }
        }
    }

    // 请求文章列表数据
    private void reqData() {
        p.reqBookListByPos(posId, 40, 1);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            articleList.clear();
            PageModel2<AdvertContent<Article>> articlePage = (PageModel2<AdvertContent<Article>>) data;

            List<AdvertContent<Article>> adArticleList = articlePage.getList();
            for (int i = 0; i < adArticleList.size(); i++) {
                AdvertContent<Article> adArticle = adArticleList.get(i);
                articleList.add(adArticle.getInfoObject());// 封装新的读书会对象
                articleList.get(i).setSmallImg(adArticle.getImgUrl());// 修改读书会图片url为外层的广告图片地址
            }
            articleAdapter.setDataList(articleList);
            if (isDataEmpty(articleList)) {
                tv_more.setText("暂无相关文章");
            } else {
                tv_more.setText("查看更多文章");
            }
        }
    }
}
